import { _decorator, Component, Node } from 'cc';
const { ccclass, property } = _decorator;

/**
 * Predefined variables
 * Name = Bullet
 * DateTime = Fri Jan 07 2022 20:50:54 GMT+0800 (中国标准时间)
 * Author = Kesa
 * FileBasename = Bullet.ts
 * FileBasenameNoExtension = Bullet
 * URL = db://assets/Scripts/bullet/Bullet.ts
 * ManualUrl = https://docs.cocos.com/creator/3.3/manual/zh/
 *
 */

@ccclass('Bullet')
export class Bullet extends Component {
    // @property
    public bulletSpeed = 60;

    /**
     *  最大飞行距离, 超过即销毁子弹实例
     */
    @property
    public OUTOFRANGE = 200;

    start() {}

    update(deltaTime: number) {
        // 在 update 中飞行
        const pos = this.node.position;
        const moveLength = pos.z - this.bulletSpeed * deltaTime;
        this.node.setPosition(pos.x, pos.y, moveLength);

        // 飞行超过最大飞行距离即销毁
        if (moveLength < -this.OUTOFRANGE) {
            this.node.destroy();
        }
    }

    onDestroy() {
        console.log('bullet destroy');
    }
}
